package com.answer.controller;


import com.answer.model.dto.app.AppAddDTO;
import com.answer.model.dto.app.AppEditDTO;
import com.answer.model.dto.app.AppQueryDTO;
import com.answer.model.dto.app.AppUpdateDTO;
import com.answer.model.reslut.Result;
import com.answer.model.reslut.ResultPage;
import com.answer.model.vo.AppVo;
import com.answer.service.AppService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/app")
public class AppController {

    @Resource
    private AppService appService;


    //查询列表
    @PostMapping("/query/all")
    public Result<ResultPage<AppVo>> queryAllApp(@RequestBody AppQueryDTO appQueryDTO) {
        ResultPage<AppVo> appVoResultPage = appService.queryAll(appQueryDTO);
        return Result.success(appVoResultPage);
    }


    //根据id查询
    @GetMapping("/query/one/{id}")
    public Result<AppVo> queryOneByIdApp(@PathVariable Long id) {
        AppVo appVo = appService.queryOneById(id);
        return Result.success(appVo);
    }


    //新增
    @PostMapping("/add")
    public Result addApp(@RequestBody AppAddDTO appAddDTO) {
        Boolean res = appService.addApp(appAddDTO);
        return res ? Result.success("添加成功") : Result.faile("添加失败");
    }

    //修改
    @PostMapping("/update")
    public Result updateApp(@RequestBody AppUpdateDTO appUpdateDTO) {
        Boolean res = appService.updateApp(appUpdateDTO);
        return res ? Result.success("修改成功") : Result.faile("修改失败");
    }


    //用户编辑
    @PostMapping("/edit")
    public Result editAPP(@RequestBody AppEditDTO appEditDTO) {
        Boolean res = appService.editApp(appEditDTO);
        return res ? Result.success("修改成功") : Result.faile("修改失败");
    }


    //删除
    @PostMapping("/delete")
    public Result deleteAPP(Long id) {
        Boolean res = appService.removeById(id);
        return res ? Result.success("删除成功") : Result.faile("删除失败");
    }


    @GetMapping("/appIdName")
    public Result getAppIdAndNameMap() {
        List<HashMap<String, String>> res = appService.getAppIdAndNameMap();
        return Result.success(res);
    }


    @PostMapping("/tetst")
    public String tetst(@RequestAttribute("ttt") String id) {
        return id;
    }

}
